﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using KinectGestureRecognition;
using System.Diagnostics;
using System.Threading;

namespace CIM_Showroom
{
    /// <summary>
    /// Interaktionslogik für Videoplayer.xaml
    /// </summary>
    public partial class Videoplayer : Window
    {
        
        // This must be hard coded
        // Video has to be linked absolut in .net 4 :-( 
        // otherwise the uri path can not be found

        String VideoLink = "D:\\+++DropBox+++\\Dropbox\\+++ projekt +++\\CIM-Showroom\\CIM-Showroom\\Videos\\Testvideo.wmv";

        #region members
        private DispatcherTimer _dispatcherMouseClickTimer = new DispatcherTimer();
        private Border _focusedBorder;
        KinectSensorModul ksm;
        #endregion

        #region constructor
        public Videoplayer()
        {
            InitializeComponent();
            this.ksm = KinectSensorModul.Instance;
            this.myMediaElement.Loaded += new RoutedEventHandler(myMediaElement_Loaded);
            //ksm.OnSkeletonFrameReadyEvent += new KinectSensorModul.SkeletonFrameReadyEvent(ksm_OnSkeletonFrameReadyEvent);
            //ksm.OnColorstreamReadyEvent += new KinectSensorModul.ColorStreamReadyEvent(ksm_OnColorstreamReadyEvent);
            _dispatcherMouseClickTimer.Interval = TimeSpan.FromMilliseconds(650);
            _dispatcherMouseClickTimer.Tick += new EventHandler(_dispatcherMouseClickTimer_Tick);
            this.imageSkeleton.Source = ksm.SkeletonFrameProp;
        }

        ~Videoplayer()
        {
            UnregisterEvents();
        }
        #endregion

        #region unsubscribe events
        void UnregisterEvents()
        {
            // this.myMediaElement.Loaded -= new RoutedEventHandler(myMediaElement_Loaded);
           // ksm.OnSkeletonFrameReadyEvent -= new KinectSensorModul.SkeletonFrameReadyEvent(ksm_OnSkeletonFrameReadyEvent);
            //ksm.OnColorstreamReadyEvent -= new KinectSensorModul.ColorStreamReadyEvent(ksm_OnColorstreamReadyEvent);
            _dispatcherMouseClickTimer.Tick -= new EventHandler(_dispatcherMouseClickTimer_Tick);
            this.Closing += new System.ComponentModel.CancelEventHandler(Videoplayer_Closing);
      
        }
        #endregion

        #region subscribed events
        void Videoplayer_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            UnregisterEvents();
        }
        void _dispatcherMouseClickTimer_Tick(object sender, EventArgs e)
        {

            // Simulate Mouse Click Action
            this._focusedBorder.Background = Brushes.Green;
            Thread.Sleep(600);
            _dispatcherMouseClickTimer.Stop();
            if (this._focusedBorder.Name.Equals("borderPlay"))
            {
                this.myMediaElement.Visibility = Visibility.Visible;
                this.borderPause.Visibility = Visibility.Visible;
                this.borderStop.Visibility = Visibility.Visible;
                this.myMediaElement.Play();
            }
            else if (this._focusedBorder.Name.Equals("borderStop"))
            {
                this.borderPause.Visibility = Visibility.Hidden;
                this.myMediaElement.Stop();
                this.myMediaElement.Visibility = Visibility.Hidden;
                this.borderPause.Visibility = Visibility.Hidden;
            }
            if (this._focusedBorder.Name.Equals("borderAbort"))
            {
                this.borderStop.Visibility = Visibility.Hidden;
                this.myMediaElement.Close();
                this.myMediaElement = null;
                this.Close();
            }
            if (this._focusedBorder.Name.Equals("borderPause"))
            {
                this.myMediaElement.Pause();
   
            }


        }
        void ksm_OnColorstreamReadyEvent(object sender, BitmapSource colorFrame)
        {
            
        }

        void ksm_OnSkeletonFrameReadyEvent(object sender, ImageSource skeletonFrame)
        {
            this.imageSkeleton.Source = skeletonFrame;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
        #endregion

        #region react 2 gui events
        private void borderIN(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Border IN");
            this._focusedBorder = ((Border)sender);
            this._focusedBorder.BorderBrush = Brushes.Green;
            // Activate Mouse Click Timer
            this.MouseClickStart();
        }

        private void borderOUT(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Border Out");
            this._focusedBorder = ((Border)sender);
            this._focusedBorder.BorderBrush = Brushes.Black;
            this._focusedBorder.Background = Brushes.Black;
            this.MouseClickAbort();
        }
        void myMediaElement_Loaded(object sender, RoutedEventArgs e)
        {
            //this.myMediaElement.Source = new Uri("D:\\+++DropBox+++\\Dropbox\\+++ projekt +++\\CIM-Showroom\\CIM-Showroom\\Videos\\Wildlife.wmv", UriKind.Relative);

            try
            {
               // this.myMediaElement.Source = new Uri(@"/../../Videos/Video.wmv", UriKind.RelativeOrAbsolute);
                this.myMediaElement.Source = new Uri(VideoLink, UriKind.Relative);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Videosource Problem: \n" + ex.Message);
            }
            this.myMediaElement.Play();
        }
        #endregion

        #region Helper Functions
        void MouseClickAbort()
        {
            this._dispatcherMouseClickTimer.Stop();
        }

        void MouseClickStart()
        {
            this._dispatcherMouseClickTimer.Start();
        }
        #endregion

        
    }
}
